package okw.gui;

import okw.network.ConnectionO;
import okw.network.StanO;
import okw.network.NonresidentThread;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import core.messages.NonresidentMsg;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class NonresidentWindow {
	Shell shell = null;
	Group dane = null;
	ConnectionO cm = null;
	StanO stanO = null;
	String election = null;
	
	public NonresidentWindow (Shell parent, final ConnectionO cm, final String election, final StanO stanO ){
		shell  = new Shell(parent, SWT.TITLE|SWT.SYSTEM_MODAL| SWT.MAX);
		this.cm = cm;
		this.stanO = stanO;
		this.election = election;
		dane = new Group(shell, SWT.NONE); 
		
		Label label = new Label(dane, SWT.CENTER);
		label.setText("Podaj id zamiejscowego glosujacego");
		label.setLocation(20,20);
		label.pack();
		
		final Text txt = new Text(dane, SWT.BORDER);
		txt.setLocation(20,40);
		txt.setSize(20,100);
		txt.pack();
		
		Button button = new Button(dane, SWT.PUSH);
		button.setText("Sprawdz");
		button.setLocation(20,70);
		button.pack();
		
		Button button2 = new Button(dane, SWT.PUSH);
		button2.setText("Anuluj");
		button2.setLocation(20,100);
		button2.pack();
		
		button.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				String text = txt.getText();
				ExecutorService service = null;
		        Future<NonresidentMsg> task;
				if(text!= null && !text.equals("")){
					
			        service = Executors.newFixedThreadPool(1);        
			        task = service.submit(new NonresidentThread(cm, election, txt.getText(), stanO));
					  
		            try {
		            	final NonresidentMsg odp;
						odp = task.get();
						if(odp == null){
							MessageBox mb = new MessageBox(shell);
							mb.setMessage("Brak polaczenia, brak id lub juz glosowal");
							mb.open();
						} else {
							dane.dispose();
							wyswietl(odp, text);
						}
	
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
		          
			        
				} else {
					MessageBox mb = new MessageBox(shell);
					mb.setMessage("Podaj poprawne id");
					mb.open();
				}
				if(service != null)
					service.shutdownNow();
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});
		
		button2.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				shell.close();
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});
	
		dane.pack();
		shell.pack();
		
		MainWindowO.center(shell);
		shell.open();
	}
	
	
	private void wyswietl(NonresidentMsg odp, final String id){
		dane = new Group(shell, SWT.NONE); 
		
		Label label = new Label(dane, SWT.CENTER);
		label.setText("Dane otrzymane z ckw" +
				"\nID: " + odp.getId() +
				"\nImie: " + odp.getImie() +
				"\nNazwisko: " + odp.getNazwisko() +
				"\nAdres: " + odp.getAdres());

		label.setLocation(20,20);
		label.pack();
		

		Button button = new Button(dane, SWT.PUSH);
		button.setText("Potwierdz");
		button.setLocation(20,100);
		button.pack();
		
		Button button2 = new Button(dane, SWT.PUSH);
		button2.setText("Anuluj");
		button2.setLocation(20,130);
		button2.pack();
		
		button.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				NonresidentMsg zm = new NonresidentMsg(stanO.getNr(), id, election, 2);
				try {
					cm.sendUp(zm);
					shell.close();
				} catch (Exception e) {
					MessageBox mb = new MessageBox(shell);
					mb.setMessage("Blad polaczenia. Poczekaj i sprobuj ponownie");
					mb.open();
				}
				
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {
				
			}
		});
		
		button2.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				NonresidentMsg zm = new NonresidentMsg(stanO.getNr(), id, election, -1);
				try {
					cm.sendUp(zm);
					shell.close();
				} catch (Exception e) {
					MessageBox mb = new MessageBox(shell);
					mb.setMessage("Blad polaczenia. Poczekaj i sprobuj ponownie");
					mb.open();
				}
				
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}
		});
		
		dane.pack();
		shell.pack();
	}
}
	
	